package com.mall.service.impl;

import com.mall.mapper.GoodsMapper;
import com.mall.po.Goods;
import com.mall.po.GoodsExample;
import com.mall.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author XueWanglin
 * @ClassName GoodsServiceImpl
 * @Description TODO
 * @Date 2021/1/6 16:40
 * @Version 1.0
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired(required = false)
    private GoodsMapper goodsMapper;
    private GoodsExample goodsExample = new GoodsExample();
    private GoodsExample.Criteria criteria = goodsExample.createCriteria();

    /**
     * 查询商品列表
     * @param page 页码
     * @return 商品集合
     */
    @Override
    public List<Goods> getGoodsListByPage(int page, int num) {
//        GoodsExample goodsExample = new GoodsExample();
//        GoodsExample.Criteria criteria = goodsExample.createCriteria();
//        criteria.andGoodsidBetween((page-1)*num,num);

        List<Goods> list = goodsMapper.selectGoodsListByPage((page-1)*num, num);
        return list;
    }

    /**
     * 查询单个商品
     * @param goods 商品实例
     * @return 商品实例
     */
    @Override
    public Goods getOneByAnyColumn(Goods goods) {
//        GoodsExample goodsExample = new GoodsExample();
//        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        if (goods.getGoodsname()!=null){
            criteria.andGoodsnameEqualTo(goods.getGoodsname());
        }else if (goods.getGoodsnumber()!=null){
            criteria.andGoodsnumberEqualTo(goods.getGoodsnumber());
        }else{
            criteria.andGoodspriceEqualTo(goods.getGoodsprice());
        }
        Goods goods1 = goodsMapper.selectByExample(goodsExample).get(0);
        return goods1;
    }

    /**
     * 添加商品
     * @param goods 商品实例
     * @return 商品实例
     */
    @Override
    public Goods addGoodsByExample(Goods goods) {
        int result = goodsMapper.insertSelective(goods);
        if(result>0){
//            GoodsExample goodsExample = new GoodsExample();
            criteria.andGoodsnameEqualTo(goods.getGoodsname());
//            criteria.andGoodsnumberEqualTo(goods.getGoodsnumber());
            Goods goods1 = goodsMapper.selectByExample(goodsExample).get(0);
            return goods1;
        }
        return null;
    }

    /**
     * 通过id删除商品
     * @param id 商品id
     * @return 是否成功
     */
    @Override
    public boolean delGoodsById(int id) {
        int result = goodsMapper.deleteByPrimaryKey(id);
        return result>0;
    }

    /**
     * 更新商品数据
     * @param goods 商品实例
     * @return 是否成功
     */
    @Override
    public boolean updateGoodsByExample(Goods goods) {

        int result = goodsMapper.updateByPrimaryKeySelective(goods);
        return result>0;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public boolean delGoodsListById(Integer[] ids) {
        int result = goodsMapper.delGoodsListById(ids);
        return result==ids.length;
    }
}
